package ar.com.larctest.adapters;

import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.List;

import ar.com.larctest.R;
import ar.com.larctest.entities.temp.TMeasurementJoin;
import ar.com.larctest.utils.Constants;

public class ListViewMeasurementDataAdapter extends ArrayAdapter<TMeasurementJoin>
{
    private static final String TAG = "ListViewMeasurementDataAdapter";

	public List<TMeasurementJoin> measurementsItems;
	Activity activity;

	public ListViewMeasurementDataAdapter(Activity activity, int textViewResourceId, List<TMeasurementJoin> measurementsItems) {
		super(activity, textViewResourceId, measurementsItems);
		this.measurementsItems = measurementsItems;
		this.activity = activity;
	}

	@Override
	public int getCount() {
		return measurementsItems.size();
	}

	@Override
	public TMeasurementJoin getItem(int position) {
		return measurementsItems.get(position);
	}

	@Override
	public long getItemId(int position) {
		return (measurementsItems.get(position)).getIdMeasurementItem();
	}

	private class ViewHolder {
        ImageView imageSelected;
	    TextView description;
        TextView time;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		LayoutInflater inflater =  activity.getLayoutInflater();
        ViewHolder holder;

		if (convertView == null)
		{
			convertView = inflater.inflate(R.layout.listitem_measurement_data, null);
			holder = new ViewHolder();
			holder.imageSelected = (ImageView) (convertView != null ? convertView.findViewById(R.id.imgSelected) : null);
			holder.description = (TextView) (convertView != null ? convertView.findViewById(R.id.description) : null);
            holder.time = (TextView) (convertView != null ? convertView.findViewById(R.id.time) : null);

            if (convertView != null) {
                convertView.setTag(holder);
            }
        }
		else
			holder = (ViewHolder) convertView.getTag();

        try
        {
            SimpleDateFormat sdf = new SimpleDateFormat(Constants.FORMAT_SHORT_TIME);
            TMeasurementJoin measurementItem = measurementsItems.get(position);

            if(measurementItem.getSelected()) {
                if(activity.getClass().getSimpleName() == "PopUpListHorsesActivity")
                    holder.imageSelected.setImageResource(R.drawable.ic_sel_item_white);
                else
                    holder.imageSelected.setImageResource(R.drawable.ic_sel_item);
            }
            else {
                if(activity.getClass().getSimpleName() == "PopUpListHorsesActivity")
                    holder.imageSelected.setImageResource(R.drawable.ic_unsel_item_white);
                else
                    holder.imageSelected.setImageResource(R.drawable.ic_unsel_item);
            }

            holder.description.setText(measurementItem.getName());
            holder.time.setText(sdf.format(measurementItem.getDateMeasurement()));

            return convertView;
        }
        catch (Exception e) {
            Log.e(TAG, e.getMessage());
            return null;
        }
	}
}
